# Avoid multiple calls to find_package to append duplicated properties to the targets
include_guard()########### VARIABLES #######################################################################
#############################################################################################
set(thrift_FRAMEWORKS_FOUND_RELEASE "") # Will be filled later
conan_find_apple_frameworks(thrift_FRAMEWORKS_FOUND_RELEASE "${thrift_FRAMEWORKS_RELEASE}" "${thrift_FRAMEWORK_DIRS_RELEASE}")

set(thrift_LIBRARIES_TARGETS "") # Will be filled later


######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
if(NOT TARGET thrift_DEPS_TARGET)
    add_library(thrift_DEPS_TARGET INTERFACE IMPORTED)
endif()

set_property(TARGET thrift_DEPS_TARGET
             PROPERTY INTERFACE_LINK_LIBRARIES
             $<$<CONFIG:Release>:${thrift_FRAMEWORKS_FOUND_RELEASE}>
             $<$<CONFIG:Release>:${thrift_SYSTEM_LIBS_RELEASE}>
             $<$<CONFIG:Release>:Boost::headers;openssl::openssl;ZLIB::ZLIB;thrift::thrift;libevent::libevent;thrift::thrift>
             APPEND)

####### Find the libraries declared in cpp_info.libs, create an IMPORTED target for each one and link the
####### thrift_DEPS_TARGET to all of them
conan_package_library_targets("${thrift_LIBS_RELEASE}"    # libraries
                              "${thrift_LIB_DIRS_RELEASE}" # package_libdir
                              thrift_DEPS_TARGET
                              thrift_LIBRARIES_TARGETS  # out_libraries_targets
                              "_RELEASE"
                              "thrift")    # package_name

# FIXME: What is the result of this for multi-config? All configs adding themselves to path?
set(CMAKE_MODULE_PATH ${thrift_BUILD_DIRS_RELEASE} ${CMAKE_MODULE_PATH})

########## COMPONENTS TARGET PROPERTIES Release ########################################

    ########## COMPONENT thriftnb::thriftnb #############

        set(thrift_thriftnb_thriftnb_FRAMEWORKS_FOUND_RELEASE "")
        conan_find_apple_frameworks(thrift_thriftnb_thriftnb_FRAMEWORKS_FOUND_RELEASE "${thrift_thriftnb_thriftnb_FRAMEWORKS_RELEASE}" "${thrift_thriftnb_thriftnb_FRAMEWORK_DIRS_RELEASE}")

        set(thrift_thriftnb_thriftnb_LIBRARIES_TARGETS "")

        ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
        if(NOT TARGET thrift_thriftnb_thriftnb_DEPS_TARGET)
            add_library(thrift_thriftnb_thriftnb_DEPS_TARGET INTERFACE IMPORTED)
        endif()

        set_property(TARGET thrift_thriftnb_thriftnb_DEPS_TARGET
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_FRAMEWORKS_FOUND_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_SYSTEM_LIBS_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_DEPENDENCIES_RELEASE}>
                     APPEND)

        ####### Find the libraries declared in cpp_info.component["xxx"].libs,
        ####### create an IMPORTED target for each one and link the 'thrift_thriftnb_thriftnb_DEPS_TARGET' to all of them
        conan_package_library_targets("${thrift_thriftnb_thriftnb_LIBS_RELEASE}"
                                      "${thrift_thriftnb_thriftnb_LIB_DIRS_RELEASE}"
                                      thrift_thriftnb_thriftnb_DEPS_TARGET
                                      thrift_thriftnb_thriftnb_LIBRARIES_TARGETS
                                      "_RELEASE"
                                      "thrift_thriftnb_thriftnb")

        ########## TARGET PROPERTIES #####################################
        set_property(TARGET thriftnb::thriftnb
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_OBJECTS_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_LIBRARIES_TARGETS}>
                     APPEND)

        if("${thrift_thriftnb_thriftnb_LIBS_RELEASE}" STREQUAL "")
            # If the component is not declaring any "cpp_info.components['foo'].libs" the system, frameworks etc are not
            # linked to the imported targets and we need to do it to the global target
            set_property(TARGET thriftnb::thriftnb
                         PROPERTY INTERFACE_LINK_LIBRARIES
                         thrift_thriftnb_thriftnb_DEPS_TARGET
                         APPEND)
        endif()

        set_property(TARGET thriftnb::thriftnb PROPERTY INTERFACE_LINK_OPTIONS
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_LINKER_FLAGS_RELEASE}> APPEND)
        set_property(TARGET thriftnb::thriftnb PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_INCLUDE_DIRS_RELEASE}> APPEND)
        set_property(TARGET thriftnb::thriftnb PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_COMPILE_DEFINITIONS_RELEASE}> APPEND)
        set_property(TARGET thriftnb::thriftnb PROPERTY INTERFACE_COMPILE_OPTIONS
                     $<$<CONFIG:Release>:${thrift_thriftnb_thriftnb_COMPILE_OPTIONS_RELEASE}> APPEND)

    ########## COMPONENT thriftz::thriftz #############

        set(thrift_thriftz_thriftz_FRAMEWORKS_FOUND_RELEASE "")
        conan_find_apple_frameworks(thrift_thriftz_thriftz_FRAMEWORKS_FOUND_RELEASE "${thrift_thriftz_thriftz_FRAMEWORKS_RELEASE}" "${thrift_thriftz_thriftz_FRAMEWORK_DIRS_RELEASE}")

        set(thrift_thriftz_thriftz_LIBRARIES_TARGETS "")

        ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
        if(NOT TARGET thrift_thriftz_thriftz_DEPS_TARGET)
            add_library(thrift_thriftz_thriftz_DEPS_TARGET INTERFACE IMPORTED)
        endif()

        set_property(TARGET thrift_thriftz_thriftz_DEPS_TARGET
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_FRAMEWORKS_FOUND_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_SYSTEM_LIBS_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_DEPENDENCIES_RELEASE}>
                     APPEND)

        ####### Find the libraries declared in cpp_info.component["xxx"].libs,
        ####### create an IMPORTED target for each one and link the 'thrift_thriftz_thriftz_DEPS_TARGET' to all of them
        conan_package_library_targets("${thrift_thriftz_thriftz_LIBS_RELEASE}"
                                      "${thrift_thriftz_thriftz_LIB_DIRS_RELEASE}"
                                      thrift_thriftz_thriftz_DEPS_TARGET
                                      thrift_thriftz_thriftz_LIBRARIES_TARGETS
                                      "_RELEASE"
                                      "thrift_thriftz_thriftz")

        ########## TARGET PROPERTIES #####################################
        set_property(TARGET thriftz::thriftz
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_OBJECTS_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_LIBRARIES_TARGETS}>
                     APPEND)

        if("${thrift_thriftz_thriftz_LIBS_RELEASE}" STREQUAL "")
            # If the component is not declaring any "cpp_info.components['foo'].libs" the system, frameworks etc are not
            # linked to the imported targets and we need to do it to the global target
            set_property(TARGET thriftz::thriftz
                         PROPERTY INTERFACE_LINK_LIBRARIES
                         thrift_thriftz_thriftz_DEPS_TARGET
                         APPEND)
        endif()

        set_property(TARGET thriftz::thriftz PROPERTY INTERFACE_LINK_OPTIONS
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_LINKER_FLAGS_RELEASE}> APPEND)
        set_property(TARGET thriftz::thriftz PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_INCLUDE_DIRS_RELEASE}> APPEND)
        set_property(TARGET thriftz::thriftz PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_COMPILE_DEFINITIONS_RELEASE}> APPEND)
        set_property(TARGET thriftz::thriftz PROPERTY INTERFACE_COMPILE_OPTIONS
                     $<$<CONFIG:Release>:${thrift_thriftz_thriftz_COMPILE_OPTIONS_RELEASE}> APPEND)

    ########## COMPONENT thrift::thrift #############

        set(thrift_thrift_thrift_FRAMEWORKS_FOUND_RELEASE "")
        conan_find_apple_frameworks(thrift_thrift_thrift_FRAMEWORKS_FOUND_RELEASE "${thrift_thrift_thrift_FRAMEWORKS_RELEASE}" "${thrift_thrift_thrift_FRAMEWORK_DIRS_RELEASE}")

        set(thrift_thrift_thrift_LIBRARIES_TARGETS "")

        ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
        if(NOT TARGET thrift_thrift_thrift_DEPS_TARGET)
            add_library(thrift_thrift_thrift_DEPS_TARGET INTERFACE IMPORTED)
        endif()

        set_property(TARGET thrift_thrift_thrift_DEPS_TARGET
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_FRAMEWORKS_FOUND_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_SYSTEM_LIBS_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_DEPENDENCIES_RELEASE}>
                     APPEND)

        ####### Find the libraries declared in cpp_info.component["xxx"].libs,
        ####### create an IMPORTED target for each one and link the 'thrift_thrift_thrift_DEPS_TARGET' to all of them
        conan_package_library_targets("${thrift_thrift_thrift_LIBS_RELEASE}"
                                      "${thrift_thrift_thrift_LIB_DIRS_RELEASE}"
                                      thrift_thrift_thrift_DEPS_TARGET
                                      thrift_thrift_thrift_LIBRARIES_TARGETS
                                      "_RELEASE"
                                      "thrift_thrift_thrift")

        ########## TARGET PROPERTIES #####################################
        set_property(TARGET thrift::thrift
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_OBJECTS_RELEASE}>
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_LIBRARIES_TARGETS}>
                     APPEND)

        if("${thrift_thrift_thrift_LIBS_RELEASE}" STREQUAL "")
            # If the component is not declaring any "cpp_info.components['foo'].libs" the system, frameworks etc are not
            # linked to the imported targets and we need to do it to the global target
            set_property(TARGET thrift::thrift
                         PROPERTY INTERFACE_LINK_LIBRARIES
                         thrift_thrift_thrift_DEPS_TARGET
                         APPEND)
        endif()

        set_property(TARGET thrift::thrift PROPERTY INTERFACE_LINK_OPTIONS
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_LINKER_FLAGS_RELEASE}> APPEND)
        set_property(TARGET thrift::thrift PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_INCLUDE_DIRS_RELEASE}> APPEND)
        set_property(TARGET thrift::thrift PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_COMPILE_DEFINITIONS_RELEASE}> APPEND)
        set_property(TARGET thrift::thrift PROPERTY INTERFACE_COMPILE_OPTIONS
                     $<$<CONFIG:Release>:${thrift_thrift_thrift_COMPILE_OPTIONS_RELEASE}> APPEND)

    ########## AGGREGATED GLOBAL TARGET WITH THE COMPONENTS #####################
    set_property(TARGET thrift::thrift-conan-do-not-use PROPERTY INTERFACE_LINK_LIBRARIES thriftnb::thriftnb APPEND)
    set_property(TARGET thrift::thrift-conan-do-not-use PROPERTY INTERFACE_LINK_LIBRARIES thriftz::thriftz APPEND)
    set_property(TARGET thrift::thrift-conan-do-not-use PROPERTY INTERFACE_LINK_LIBRARIES thrift::thrift APPEND)

########## For the modules (FindXXX)
set(thrift_LIBRARIES_RELEASE thrift::thrift-conan-do-not-use)
